package com.btr.ivle.helper;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.btr.ivle.R;
import com.btr.ivle.data.IVLEToken;

public class WorkbinSeparatedListAdapter extends ArrayAdapter<Map<String,String>> {
	
	private Context context;
	
    public WorkbinSeparatedListAdapter(Context context, List<Map<String, String>> list) {
        super(context, 0, list);
        this.context = context;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	View view = convertView;
    	DatabaseWorkbin db = new DatabaseWorkbin(context,IVLEToken.getUserID() + "_Workbin",null,context.getResources().getInteger(R.integer.DBVersion));
        if(getItem(position).containsKey("Tag")){
            view = LayoutInflater.from(getContext()).inflate(R.layout.group_list_item_tag, null);
            TextView tvTitle = (TextView) view.findViewById(R.id.group_list_item_text);
            tvTitle.setText(getItem(position).get("Tag"));
        }else if(getItem(position).containsKey(db.FolderName)){
            view = LayoutInflater.from(getContext()).inflate(R.layout.group_list_item_workbin, null);
            if(getItem(position).containsKey(db.isDownloaded)){
            	ImageView im = (ImageView) view.findViewById(R.id.icon);
            	im.setImageResource(R.drawable.folder_new);
            }
            TextView tvTitle = (TextView) view.findViewById(R.id.title);
            tvTitle.setText(getItem(position).get(db.FolderName));
            TextView tvTime = (TextView) view.findViewById(R.id.time);
            tvTime.setText(getItem(position).get(db.OpenDate));
            
        }else if(getItem(position).containsKey(db.FileName)){
            view = LayoutInflater.from(getContext()).inflate(R.layout.group_list_item_workbin, null);
            if(getItem(position).containsKey(db.isDownloaded)){
            	ImageView imNotD = (ImageView) view.findViewById(R.id.notyet_downloaded);
            	imNotD.setVisibility(View.VISIBLE);
            }
            TextView tvTitle = (TextView) view.findViewById(R.id.title);
            tvTitle.setText(getItem(position).get(db.FileName));
            TextView tvTime = (TextView) view.findViewById(R.id.time);
            tvTime.setText(getItem(position).get(db.UploadTime));
            
            ImageView im = (ImageView) view.findViewById(R.id.icon);
            if(getItem(position).get(db.FileType).equals("doc")||
            		getItem(position).get(db.FileType).equals("docx"))
            	im.setImageResource(R.drawable.doc);
            else if(getItem(position).get(db.FileType).equals("xls")||
            		getItem(position).get(db.FileType).equals("xlsx"))
            	im.setImageResource(R.drawable.xls);
            else if(getItem(position).get(db.FileType).equals("ppt")||
            		getItem(position).get(db.FileType).equals("pptx"))
            	im.setImageResource(R.drawable.ppt);
            else if(getItem(position).get(db.FileType).equals("zip")||
            		getItem(position).get(db.FileType).equals("rar"))
            	im.setImageResource(R.drawable.zip);
            else if(getItem(position).get(db.FileType).equals("html")||
            		getItem(position).get(db.FileType).equals("htm")||
            		getItem(position).get(db.FileType).equals("php")||
            		getItem(position).get(db.FileType).equals("asp"))
            	im.setImageResource(R.drawable.link);
            else if(getItem(position).get(db.FileType).equals("png")||
            		getItem(position).get(db.FileType).equals("jpg")||
            		getItem(position).get(db.FileType).equals("bmp")||
            		getItem(position).get(db.FileType).equals("gif"))
            	im.setImageResource(R.drawable.picture);
            else if(getItem(position).get(db.FileType).equals("pdf"))
            	im.setImageResource(R.drawable.pdf);
            else im.setImageResource(R.drawable.file);
        }
        return view;
    }
    
    @Override
    public boolean isEnabled(int position) {
        if(getItem(position).containsKey("Tag")){
            return false;
        }
        return super.isEnabled(position);
    }
}
